package com.bbzn.device.client.controller;

import com.bbzn.device.client.vo.req.FireImageVO;
import com.bbzn.device.client.vo.res.FireIndexVO;
import com.bbzn.device.client.aspect.OperationAnnotation;
import com.bbzn.device.client.constant.CrudConstant;
import com.bbzn.device.client.constant.DeviceConstant;
import com.bbzn.device.client.dataobject.DeviceProcess;
import com.bbzn.device.client.dataobject.FireImage;
import com.bbzn.device.client.dataobject.SystemParam;
import com.bbzn.device.client.exception.DefinitException;
import com.bbzn.device.client.service.*;
import com.bbzn.device.client.utils.BaseApiService;
import com.bbzn.device.client.utils.BaseResponse;
import com.bbzn.device.client.utils.BeanVOUtils;
import com.bbzn.device.client.utils.MayiktJwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/firesite")
@Slf4j
public class FireIndexController extends BaseApiService {

    @Autowired
    private CommonService commonService;


    @Autowired
    private DeviceService deviceService;

    @Autowired
    private FireImageService fireImageService;

    @Autowired
    private OssService ossService;

    @Autowired
    private SystemParamByCacheService systemParamByCacheService;

    @Autowired
    private DeviceProcessService deviceProcessService;

    @Autowired
    private IndexService indexService;

    /**
     * 返回主页数据
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getFireSiteIndex", method = RequestMethod.POST)
    @OperationAnnotation(content = "获取消防首页")
    public BaseResponse getFireSiteIndex(HttpServletRequest request) throws Exception {
        SystemParam systemParam = null;
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        Long userId = commonService.getUserId(request);
        DeviceProcess deviceProcess = new DeviceProcess();
        deviceProcess.setOperatorId(1L);
        List<DeviceProcess> deviceProcessList = deviceProcessService.findByAll(deviceProcess);
        List<Long> userIds = null;
        if(CollectionUtils.isNotEmpty(deviceProcessList)){
            userIds = deviceProcessList.stream().filter(BeanVOUtils.distinctByKey(DeviceProcess::getReceiveId)).map(DeviceProcess::getReceiveId).collect(Collectors.toList());
        }else{
            userIds = commonService.getUserIds(1L);
        }
        if(CollectionUtils.isEmpty(userIds)){
            systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_NO_ID_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        FireIndexVO fireIndexVO = indexService.parseFireIndexVO(userIds,userId);
        return setResultSuccess(fireIndexVO);
    }

    /**
     * 信息录入
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/uploadFireImage", method = RequestMethod.POST)
    @OperationAnnotation(content = "信息录入")
    public BaseResponse uploadFireImage(@RequestBody FireImageVO fireImageVO,HttpServletRequest request) throws Exception {
        SystemParam systemParam = null;
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        Long userId = commonService.getUserId(request);
        String username = MayiktJwtUtils.getUsername(request,secretKey);
        if(null==fireImageVO.getId()){
            FireImage fireImage = new FireImage();
            fireImage.setFireMapUrl(fireImageVO.getFireMapUrl());
            fireImage.setUserId(userId);
            fireImage.setCreateTime(new Date());
            fireImage.setCreateBy(username);
            fireImageService.insertSelective(fireImage);
        }else{
            FireImage fireImage = new FireImage();
            fireImage.setId(fireImageVO.getId());
            fireImage.setFireMapUrl(fireImageVO.getFireMapUrl());
            fireImage.setUserId(userId);
            fireImage.setUpdateTime(new Date());
            fireImage.setUpdateBy(username);
            fireImageService.updateByPrimaryKeySelective(fireImage);
        }
        systemParam = systemParamByCacheService.getValues(CrudConstant.CRUD_ENUM, CrudConstant.MODIFY_SUCCESS_PREFIX + language);
        return setResultSuccess(systemParam.getMassage());
    }
}
